home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 February / PC_Format_022008.iso / Internet / Mozilla Thunderbird wtyczki / lightning-0.7-tb-win.xpi / js / calWcapRequest.js < prev    next >
Encoding:
JavaScript  |  2007-09-13  |  14.8 KB  |  435 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Sun Microsystems code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Sun Microsystems, Inc.
  19.  * Portions created by the Initial Developer are Copyright (C) 2007
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /**
  40.    A request object is used to track an async action.
  41.    While the action is running, isPending is true.
  42.    Functions issuing an async action usually take a response function along
  43.    with their parameters, typically named respFunc.
  44.    That function is called *after* the action has ended (i.e. isPending of the
  45.    issued action/request is false when called, status remains stable).
  46.    The response function gets the ended request as first parameter to check
  47.    whether the request has been successful and get its data.
  48.    The request function itself may return either
  49.    - a further calIOperation request object, i.e. an async continuation
  50.    - some data (incl null/undefined) which is the result of the async function,
  51.      indicating that there is no further continuation
  52. */
  53.  
  54. var g_requestPrefix = null;
  55. var g_requestId = 0;
  56. function generateRequestId() {
  57.     if (!g_requestPrefix) {
  58.         g_requestPrefix = (getUUID() + "-");
  59.     }
  60.     ++g_requestId;
  61.     return (g_requestPrefix + g_requestId);
  62. }
  63.  
  64. function calWcapRequest(respFunc, logContext) {
  65.     this.wrappedJSObject = this;
  66.     this.m_logContext = logContext;
  67.     this.m_id = generateRequestId();
  68.     this.m_isPending = true;
  69.     this.m_status = NS_OK;
  70.     this.m_respFunc = respFunc;
  71.     this.m_attachedRequests = [];
  72. }
  73. calWcapRequest.prototype = {
  74.     m_logContext: null,
  75.     m_parentRequest: null,
  76.     m_id: 0,
  77.     m_isPending: true,
  78.     m_status: NS_OK,
  79.     m_respFunc: null,
  80.     m_attachedRequests: null,
  81.     m_locked: false,
  82.     
  83.     get parentRequest() { return this.m_parentRequest; },
  84.     set parentRequest(req) {
  85.         if (this.parentRequest)
  86.             logError("already has parent!", this);
  87.         this.detachFromParent(); // detach without error
  88.         return (this.m_parentRequest = req);
  89.     },
  90.     
  91.     /** The following locking is necessary when scheduling multiple async
  92.         requests; one cannot be sure that e.g. the first completes quickly
  93.         and responds the whole parent request when detaching.
  94.     */
  95.     lockPending: function calWcapRequest_lockPending() {
  96.         this.m_locked = true;
  97.     },
  98.     unlockPending: function calWcapRequest_unlockPending() {
  99.         if (this.m_locked) {
  100.             this.m_locked = false;
  101.             // assures that respFunc is executed:
  102.             if (this.m_attachedRequests.length == 0) {
  103.                 this.execRespFunc();
  104.             }
  105.         }
  106.     },
  107.     
  108.     toString: function calWcapRequest_toString() {
  109.         var ret = ("calWcapRequest id=" + this.id +
  110.                    ", parent-id=" +
  111.                    (this.parentRequest ? this.parentRequest.id : "<none>") +
  112.                    " (" + this.m_logContext + ")");
  113.         if (LOG_LEVEL > 2 && this.m_attachedRequests.length > 0) {
  114.             ret += "\nattached requests:";
  115.             for each ( var req in this.m_attachedRequests ) {
  116.                 ret += ("\n#" + req.id + "\t" + req);
  117.             }
  118.         }
  119.         ret += (", isPending=" + this.isPending);
  120.         ret += (", status=" + errorToString(this.status));
  121.         return ret;
  122.     },
  123.     
  124.     attachSubRequest: function calWcapRequest_attachSubRequest(req)
  125.     {
  126.         if (req) {
  127.             if (!this.m_attachedRequests.some(
  128.                     function(req_) { return req.id == req_.id; } )) {
  129.                 if (req.isPending) {
  130.                     this.m_attachedRequests.push(req);
  131.                     req.parentRequest = this;
  132.                     log("attachSubRequest()", this);
  133.                 }
  134.                 else if (!this.m_locked && this.m_attachedRequests.length == 0) {
  135.                     this.execRespFunc(req.status);
  136.                 }
  137.             }
  138.             else {
  139.                 logError("request already attached: " + req.id, this);
  140.             }
  141.         }
  142.     },
  143.     
  144.     detachSubRequest: function calWcapRequest_detachSubRequest(req, err)
  145.     {
  146.         this.m_attachedRequests = this.m_attachedRequests.filter(
  147.             function(req_) { return req.id != req_.id; } );
  148.         if (err) {
  149.             // first failing sub request stops parent request:
  150.             this.execRespFunc(err);
  151.         }
  152.         // assures that respFunc is executed after every sub request has been completed:
  153.         else if (!this.m_locked && this.m_attachedRequests.length == 0) {
  154.             this.execRespFunc();
  155.         }
  156.     },
  157.     
  158.     cancelAllSubRequests: function calWcapRequest_cancelAllSubRequests(status) {
  159.         var attachedRequests = this.m_attachedRequests;
  160.         this.m_attachedRequests = [];
  161.         attachedRequests.forEach( function(req) { req.cancel(null); } );
  162.     },
  163.     
  164.     detachFromParent: function calWcapRequest_detachFromParent(err) {
  165.         var parentRequest = this.m_parentRequest;
  166.         if (parentRequest) {
  167.             this.m_parentRequest = null;
  168.             parentRequest.detachSubRequest(this, err);
  169.         }
  170.     },
  171.     
  172.     execRespFunc: function calWcapRequest_execRespFunc(err, data)
  173.     {
  174.         if (this.isPending) {
  175.             this.m_isPending = false;
  176.             if (err)
  177.                 this.m_status = err;
  178.             this.cancelAllSubRequests();
  179.             var respFunc = this.m_respFunc;
  180.             if (respFunc) {
  181.                 this.m_respFunc = null; // call once only
  182.                 if (LOG_LEVEL > 2) {
  183.                     log("response exec: " + errorToString(err), this);
  184.                 }
  185.                 try {
  186.                     respFunc(this, err, data);
  187.                 }
  188.                 catch (exc) {
  189.                     this.m_status = exc;
  190.                     // don't pump into error console, may be handled:
  191.                     log("error: " + errorToString(exc), this);
  192.                 }
  193.             }
  194.             this.detachFromParent(this.m_status);
  195.         }
  196.     },
  197.     
  198.     // calIOperation:
  199.     get id() {
  200.         return this.m_id;
  201.     },
  202.     get isPending() {
  203.         return this.m_isPending;
  204.     },
  205.     get success() {
  206.         return (!this.isPending && Components.isSuccessCode( getResultCode(this.status) ));
  207.     },
  208.     get status() {
  209.         return (this.m_status === null ? NS_OK : this.m_status);
  210.     },
  211.     
  212.     cancel: function calWcapRequest_cancel(status) {
  213.         log("cancel.", this);
  214.         if (!status)
  215.             status = calIErrors.OPERATION_CANCELLED;
  216.         this.execRespFunc(status);
  217.     }
  218. };
  219.  
  220. function calWcapNetworkRequest(channel, respFunc, bLogging) {
  221.     this.wrappedJSObject = this;
  222.     this.m_id = generateRequestId();
  223.     this.m_channel = channel;
  224.     this.m_respFunc = respFunc;
  225.     this.m_bLogging = (bLogging === undefined ? true : bLogging);
  226. }
  227.  
  228. calWcapNetworkRequest.prototype = {
  229.     m_id: 0,
  230.     m_channel: null,
  231.     m_respFunc: null,
  232.     m_bLogging: false,
  233.     
  234.     toString: function calWcapNetworkRequest_toString() {
  235.         var ret = ("calWcapNetworkRequest id=" + this.id +
  236.                    ", parent-id=" +
  237.                    (this.parentRequest ? this.parentRequest.id : "<none>"));
  238.         if (this.m_bLogging)
  239.             ret += (" (" + this.m_channel.URI.spec + ")");
  240.         ret += (", isPending=" + this.isPending);
  241.         ret += (", status=" + errorToString(this.status));
  242.         return ret;
  243.     },
  244.     
  245.     m_parentRequest: null,
  246.     get parentRequest() { return this.m_parentRequest; },
  247.     set parentRequest(req) {
  248.         if (this.parentRequest)
  249.             logError("already has parent!", this);
  250.         this.detachFromParent(); // detach without error
  251.         return (this.m_parentRequest = req);
  252.     },
  253.  
  254.     // calIOperation:
  255.     get id() {
  256.         return this.m_id;
  257.     },
  258.     
  259.     m_isPending: true,
  260.     get isPending() { return this.m_isPending; },
  261.     
  262.     get success() {
  263.         return (!this.isPending && Components.isSuccessCode( getResultCode(this.status) ));
  264.     },
  265.     get status() {
  266.         return this.m_channel.status;
  267.     },
  268.     
  269.     detachFromParent: function calWcapNetworkRequest_detachFromParent(err) {
  270.         var parentRequest = this.m_parentRequest;
  271.         if (parentRequest) {
  272.             this.m_parentRequest = null;
  273.             parentRequest.detachSubRequest(this, err);
  274.         }
  275.     },
  276.     
  277.     cancel: function calWcapNetworkRequest_cancel(status) {
  278.         log("cancel.", this);
  279.         if (!status)
  280.             status = calIErrors.OPERATION_CANCELLED;
  281.         this.execRespFunc(status);
  282.         // xxx todo: check whether this works on redirected channels!
  283.         if (this.m_channel.isPending()) {
  284.             log("canceling netwerk request...", this);
  285.             this.m_channel.cancel(NS_BINDING_FAILED);
  286.         }
  287.     },
  288.     
  289.     execRespFunc: function calWcapNetworkRequest_execRespFunc(err, str)
  290.     {
  291.         if (this.isPending) {
  292.             this.m_isPending = false;
  293.             var respFunc = this.m_respFunc;
  294.             if (respFunc) {
  295.                 this.m_respFunc = null; // call once only
  296.                 if (LOG_LEVEL > 2 && this.m_bLogging) {
  297.                     log("response exec: " + errorToString(err), this);
  298.                 }
  299.                 try {
  300.                     respFunc(err, str);
  301.                     err = null; // may have been handled
  302.                 }
  303.                 catch (exc) {
  304.                     // don't pump into error console, may be handled:
  305.                     log("error: " + errorToString(exc), this);
  306.                     err = exc;
  307.                 }
  308.             }
  309.             this.detachFromParent(err);
  310.         }
  311.     },
  312.     
  313.     // nsIUnicharStreamLoaderObserver:
  314.     onDetermineCharset: function calWcapNetworkRequest_onDetermineCharset(
  315.         loader, context, firstSegment, length)
  316.     {
  317.         var channel = null;
  318.         if (loader)
  319.             channel = loader.channel;
  320.         var charset = null;
  321.         if (channel)
  322.             charset = channel.contentCharset;
  323.         if (!charset || charset.length == 0)
  324.             charset = "UTF-8";
  325.         return charset;
  326.     },
  327.     
  328.     onStreamComplete: function calWcapNetworkRequest_onStreamComplete(
  329.         loader, context, status, /* nsIUnicharInputStream */ unicharData)
  330.     {
  331.         if (LOG_LEVEL > 0 && this.m_bLogging) {
  332.             log("status: " + errorToString(status), this);
  333.         }
  334.         switch (status) {
  335.         case NS_BINDING_SUCCEEDED: {
  336.             var err = null;
  337.             var str = "";
  338.             try {
  339.                 if (unicharData) {
  340.                     var str_ = {};
  341.                     while (unicharData.readString(-1, str_)) {
  342.                         str += str_.value;
  343.                     }
  344.                 }
  345.                 if (LOG_LEVEL > 2 && this.m_bLogging) {
  346.                     log("contentCharset = " + this.onDetermineCharset(loader) +
  347.                         "\nrequest result:\n" + str, this);
  348.                 }
  349.             }
  350.             catch (exc) {
  351.                 err = exc;
  352.             }
  353.             this.execRespFunc(err, str);
  354.             break;
  355.         }
  356.         case NS_BINDING_REDIRECTED:
  357.         case NS_BINDING_RETARGETED:
  358.             // just status
  359.             // xxx todo: in case of a redirected channel,
  360.             // how to get that channel => cancel feature!
  361.             break;
  362.         default: // errors:
  363.             this.execRespFunc(status);
  364.             break;
  365.         }
  366.     }
  367. };
  368.  
  369. function issueNetworkRequest(parentRequest, respFunc, url, bLogging)
  370. {
  371.     var channel;
  372.     try {
  373.         var loader = Components.classes["@mozilla.org/network/unichar-stream-loader;1"]
  374.                                .createInstance(Components.interfaces.nsIUnicharStreamLoader);
  375.         channel = getIoService().newChannel(url, "" /* charset */, null /* baseURI */);
  376.         channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  377.     }
  378.     catch (exc) {
  379.         respFunc(exc);
  380.         return;
  381.     }
  382.     var netRequest = new calWcapNetworkRequest(channel, respFunc, bLogging);
  383.     parentRequest.attachSubRequest(netRequest);
  384.     log("opening channel.", netRequest);
  385.     try {
  386.         loader.init(channel, netRequest, null /*context*/, 0 /*segment size*/);
  387.     }
  388.     catch (exc) {
  389.         netRequest.execRespFunc(exc);
  390.     }
  391. }
  392.  
  393. function getWcapRequestStatusString(xml)
  394. {
  395.     var str = "request status: ";
  396.     var items = xml.getElementsByTagName("RSTATUS");
  397.     if (items != null && items.length > 0)
  398.         str += items.item(0).textContent;
  399.     else
  400.         str += "none";
  401.     return str;
  402. }
  403.  
  404. function stringToIcal(data, expectedErrno)
  405. {
  406.     if (!data || data.length == 0) { // assuming time-out; WTF.
  407.         throw new Components.Exception(
  408.             "Login failed. Invalid session ID.",
  409.             Components.interfaces.calIWcapErrors.WCAP_LOGIN_FAILED);
  410.     }
  411.     var icalRootComp;
  412.     try {
  413.         icalRootComp = getIcsService().parseICS(data);
  414.     }
  415.     catch (exc) { // map into more useful error string:
  416.         throw new Components.Exception("error parsing ical data!",
  417.                                        Components.interfaces.calIErrors.ICS_PARSE);
  418.     }
  419.     checkWcapIcalErrno(icalRootComp, expectedErrno);
  420.     return icalRootComp;
  421. }
  422.  
  423. function stringToXml(data, expectedErrno)
  424. {
  425.     if (!data || data.length == 0) { // assuming time-out
  426.         throw new Components.Exception(
  427.             "Login failed. Invalid session ID.",
  428.             Components.interfaces.calIWcapErrors.WCAP_LOGIN_FAILED);
  429.     }
  430.     var xml = getDomParser().parseFromString(data, "text/xml");
  431.     checkWcapXmlErrno(xml, expectedErrno);
  432.     return xml;
  433. }
  434.  
  435.